package co.edu.unal.satacredito.mediation;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import co.edu.unal.satacredito.database.administration.ejb.EstablishmentDaoEJBLocal;
import co.edu.unal.satacredito.database.administration.exceptions.DAOException;
import co.edu.unal.satacredito.database.entity.Establishment;
import co.edu.unal.satacredito.infrastructure.dto.EstablishmentDTO;

/**
 * Session Bean implementation class BrandEJB
 */

@Stateless
public class EstablishmentEJB implements EstablishmentEJBRemote, EstablishmentEJBLocal {

	@EJB
	EstablishmentDaoEJBLocal establishmentDaoEJBLocal;
    
    public EstablishmentEJB() {
    	
    }
    
    @Override
    public List<EstablishmentDTO> findAllEstablishment(){
		try {
			List<Establishment> establishments = establishmentDaoEJBLocal.findAllEstablishment();
			List<EstablishmentDTO> establishmentDTO = new ArrayList<EstablishmentDTO>();
			
			for (Establishment establishment : establishments) {
				establishmentDTO.add(new EstablishmentDTO(establishment));
			}
			
			return establishmentDTO;
			
		} catch (DAOException e) {
			return new ArrayList<EstablishmentDTO>();
		}
    }
    
    @Override
    public EstablishmentDTO findEstablishment(String establishmentId) throws Exception{
    	try {
    		Establishment establishment = new Establishment();
    		establishment = establishmentDaoEJBLocal.findEstablishmentById(establishmentId);
			EstablishmentDTO establishmentDTO = new EstablishmentDTO(establishment);
			return establishmentDTO;
			
		} catch (Exception e) {
			return null;
		}
    }
    
    @Override
    public void create(EstablishmentDTO establishmentDTO) throws Exception{
    	try {
    		
    		
    		
    		Establishment establishment = new Establishment();
    		establishment.setUser(establishmentDTO.getUser());
    		establishment.setPassword(establishmentDTO.getPassword());
    		establishment.setNameEstablishment(establishmentDTO.getNameEstablishment());
    		establishment.setPhoneNumber(establishmentDTO.getPhoneNumber());
    		establishment.setManagerFirstName(establishmentDTO.getManagerFirstName());
    		establishment.setManagerLastName(establishmentDTO.getManagerLastName());
    		establishment.setManagerId(establishmentDTO.getManagerId());
    		establishment.setEmail(establishmentDTO.getEmail());
    		establishment.setBrand(establishmentDTO.getBrand());
    		establishment.setStatus(establishmentDTO.getStatus());
			establishmentDaoEJBLocal.createEstablishment(establishment);
		} catch (Exception e) {
			throw e;
		}
    }
    
    @Override
    public void edit(String id, EstablishmentDTO establishmentDTO) throws Exception{
    	try {
    		Establishment establishment = new Establishment(id);
    		establishment.setUser(establishmentDTO.getUser());
    		establishment.setPassword(establishmentDTO.getPassword());
    		establishment.setNameEstablishment(establishmentDTO.getNameEstablishment());
    		establishment.setPhoneNumber(establishmentDTO.getPhoneNumber());
    		establishment.setManagerFirstName(establishmentDTO.getManagerFirstName());
    		establishment.setManagerLastName(establishmentDTO.getManagerLastName());
    		establishment.setManagerId(establishmentDTO.getManagerId());
    		establishment.setEmail(establishmentDTO.getEmail());
    		establishment.setBrand(establishmentDTO.getBrand());
    		establishment.setStatus(establishmentDTO.getStatus());
			establishmentDaoEJBLocal.editEstablishment(id, establishment);
		} catch (Exception e) {
			throw e;
		}
    }
    
    @Override
    public void delete(String id) throws Exception{
    	try {
			establishmentDaoEJBLocal.deleteEstablishment(id);
		} catch (Exception e) {
			throw e;
		}
    }

}
